import cache from '../../api/cache'
import {
  request
} from "../../requests/post.js";

const auth = require('../../utils/auth1');

const app = getApp();

Page({

  data: {
    photo_url: app.globalData.baseUrl + "/wx/TX.jpg",
    arraySex: ['女', '男'],
    user:{
      userName: null,
      sex: null,
      age: null,
      telephone: null,
      upAge: null,
      downAge: null,
      scoreAuthorize: null
    },
    listShowFlag: false,
    account: '',
    userInfo: null
  },


  onTap:function(event){
    console.info("hello")

    // wx.navigateTo({
    //   url: 'index'
    // })
  },

  onLoad: function (options) {

    var user = cache.getOp(app.globalData.userCacheKey)
    
    if (!user && user.scoreAuthorize == '1') {
      this.setData({
        'listShowFlag': false
      })
    } else {
      wx.redirectTo({
        url: 's'
      })
    }
      
      

    // var key = app.globalData.key
    // console.log("key - " + key);
    // if (!key) {
    //   console.info(" key is null")

    //   try {
    //     auth.loginAndGetUserInfo();
    //     console.log('登录成功');
    //   } catch (err) {
    //     console.error('登录失败:', err);
    //   }

    // } else {
    //   // wx.redirectTo({
    //   //   url: 'index'
    //   // })
    // }
  },

  wxLogin() {
    
    var timestamp = Date.now();
    timestamp = Math.floor(timestamp / 1000).toString();
    var nickName = "番薯用户" + timestamp
    console.log("nickname - " + nickName)

    if (nickName !== "" && nickName !== null) {
      console.log("nickname - 11")
      var that = this
      wx.login({
        success(res) {
          if (res.code) {
            request({
              url: '/xq/login',
              code: res.code,
              nickName: nickName
            }).then(result => {
              if (result.data.errorCode == 200) {
                cache.setOp(app.globalData.userCacheKey, result.data.data)
                // cache.setOp(app.globalData.tokenKey, result.data.data["token"])
                // app.globalData.token = result.data.data["token"]
                app.globalData.key = result.data.data["key"]
                that.setData({
                  user: result.data.data,
                  hasUserInfo: true,
                })
                // 跳转到指标页
                wx.hideLoading()
                that.setData({
                  'listShowFlag': true
                })
                if(result.data.data.scoreAuthorize == "1") {
                  wx.redirectTo({
                    url: 'birthday'
                  })
                }
                
              } else {
                wx.hideLoading()
                that.setData({
                  "iosDialog2": true
                })
                console.log('服务器异常')
              }
            }, err => {
              //调用服务端登录接口失败
              wx.hideLoading()
              that.setData({
                "iosDialog2": true
              })
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '昵称不能为空',
        icon: 'none',
        duration: 2000,
      })
    }
  
  },
})